set -e
resourceDir="${PROJECT_DIR}/ToBuildResources"
out=$resourceDir/out
cache=$resourceDir/cache
mkdir -p $out
mkdir -p $cache

build(){

	matc_path="${SRCROOT}/matc"

    if [[ ! -f $matc_path ]]; then
    echo "error: matc 不存在" >&2
        exit 1
    fi
     
	for file in `ls -a $resourceDir`
	do
		if [[ $file == *mat ]]; then
			absFPath=$resourceDir/$file
			fmd5=$(md5 -q $resourceDir/$file)
			fnameBase=$(basename -s .mat $file)
			fname=$(basename $file)
			md5FlagFile="$cache/$fnameBase-$fmd5"
			if [[ -f $md5FlagFile ]]; then
				echo "[using cache] for $file"
			else
				echo "[build] $file -> ${out}/${fnameBase}.filamat"
				"${matc_path}" \
				    --api all \
				    --platform mobile \
				    -o "${out}/${fnameBase}.filamat" \
				    "$absFPath"
				if [[ $? == 0 ]]; then
					touch $md5FlagFile
				else
                   echo $1
                   exit 1
                fi
			fi
		fi
	done
}


copyToBundle(){
	for file in `ls $out`
	do
		absFPath=$out/$file
		cp $absFPath "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/$file"
	done
}

if [[ $1 == "build" ]]; then
	build
elif [[ $1 == "copy" ]]; then
	copyToBundle
fi




